package com.cy.pj.sys.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.cy.pj.common.vo.JsonResult;
import com.cy.pj.common.vo.PageResult;
import com.cy.pj.sys.entity.SysMember;
import com.cy.pj.sys.service.SysMemberService;

@RestController
@RequestMapping("member/")
public class SysMemberController {

	@Autowired
	private SysMemberService sysMemberService;

	@RequestMapping("doLogin")
	public JsonResult doLogin(String memberName,String password){
		//1.获取Subject对象
		Subject subject=SecurityUtils.getSubject();
		//2.通过Subject提交用户信息,交给shiro框架进行认证操作
		//2.1对用户进行封装
		UsernamePasswordToken token=
				new UsernamePasswordToken(
						memberName,//身份信息
						password);//凭证信息
		//2.2对用户信息进行身份认证
		subject.login(token);
		//分析:
		//1)token会传给shiro的SecurityManager
		//2)SecurityManager将token传递给认证管理器
		//3)认证管理器会将token传递给realm
		System.out.println("测试");
		return new JsonResult("login ok");
	}

	/**
	 * 基于会员号码删除会员信息
	 */
	@RequestMapping("doDeleteByMobile") 
	public JsonResult doDeleteByMobile(String[] mobiles) {
		sysMemberService.deleteByMobile(mobiles);

		return new JsonResult("删除成功"); 
	}

	/**
	 * 更新会员信息
	 */
	@RequestMapping("doUpdateMember") 
	public JsonResult doUpdateMember(SysMember entity){
		sysMemberService.updateMember(entity);

		return new JsonResult("更新成功"); 
	}

	/**
	 * 会员充值
	 */
	@RequestMapping("doMemberRecharge") 
	public JsonResult doMemberRecharge(Long account,Long money,String mobile){
		sysMemberService.updateAccountByMobile(account, money,mobile);

		return new JsonResult("充值成功"); 
	}


	/**
	 * 分页查询
	 */
	@RequestMapping("doFindPageObjects")
	public JsonResult doFindPageObjects(String memberName,Integer pageCurrent){
		PageResult<SysMember> list=
				sysMemberService.findPageObjects(memberName,pageCurrent);
		return new JsonResult(list);
	}

	/**
	 * 添加会员信息
	 */
	@RequestMapping("doSaveObject")
	public JsonResult saveObject(SysMember sysMember){
		sysMemberService.saveMemberInfo(sysMember);
		return new JsonResult("添加成功");
	}

	/**
	 * 基于手机号码查询会员信息
	 */
	@RequestMapping("doFindMemberByMobile")
	public JsonResult doFindMemberByMobile(String mobile){
		SysMember list = sysMemberService.findMemberByMobile(mobile);

		return new JsonResult(list);
	}
}














